package com.nut.lmsc.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.api.req.OrderQueryReq;
import com.nut.lmsc.common.Holder;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.Order;
import com.nut.lmsc.interceptor.MerchantJwt;
import com.nut.lmsc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/merchant/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "list")
    public Result<IPage<Order>> list(OrderQueryReq req) {
        return orderService.listPage(req);
    }

    @GetMapping("/export")
    public void exportExcel(OrderQueryReq req, HttpServletResponse response) {
//        orderService.exportExcel(req, response);
    }

    @GetMapping(value = "/{orderId}")
    public Result<Order> detail(@PathVariable String orderId) {
        return orderService.detail(orderId);
    }

    @GetMapping(value = "/send/{orderId}")
    public Result<Order> send(@PathVariable String orderId) {
        MerchantJwt merchantJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        return orderService.send(orderId, merchantJwt);
    }
//
//    @PostMapping(value = "/cancel")
//    public Result<Order> cancel(@RequestBody CancelOrderReq req) {
//        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
//
//        req.setUsername(userJwt.getUsername());
//        req.setUserId(userJwt.getUserId());
//        req.setFroms("后台");
//
//        return orderService.cancel(req);
//    }

    @PutMapping
    public Result update(@RequestBody Order order) {
        return orderService.updateById(order);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return orderService.deleteByIds(ids);
    }

}